﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using GamePlugin.Controls;
using GamePlugin.Logics.Core;
using System.Windows.Controls;

namespace GamePlugin.Logics.Data
{
    public class SearchItem : PluginObject
    {
        public static readonly char _ITEM_NAME_LEFT = '[';
        public static readonly char _ITEM_NAME_RIGHT = ']';
        public static readonly char _MARK = '-';

        public SearchItem(string searchItemName = null)
        {
            //this.Occupation = Helper.GetIndex("職業", "巫醫");
            //this.EquipKind = Helper.GetIndex("裝備類型", "單手");
            //this.ItemRarity = Helper.GetIndex("物品稀有度", "稀有");
            this.ItemName = "";
            this.ItemAuctionPrice = 0;
            this.ItemBuyPrice = 0;
            this.SearchInfoName = searchItemName;
            this.UpdateSearchInfoName();

            this.IsAvailable = true;
        }

        public void CopyFrom(SearchItem item)
        {
            if (item == null)
                return;

            this.Occupation = item.Occupation;
            this.EquipKind = item.EquipKind;
            this.ItemKind = item.ItemKind;

            this.ItemName = item.ItemName;
            this.IsAvailable = item.IsAvailable;
            this.ItemAuctionPrice = item.ItemAuctionPrice;
            this.ItemBuyPrice = item.ItemBuyPrice;
            this.ItemRarity = item.ItemRarity;

            this.Property1 = item.Property1;
            this.Property1Value = item.Property1Value;
            this.Property2 = item.Property2;
            this.Property2Value = item.Property2Value;
            this.Property3 = item.Property3;
            this.Property3Value = item.Property3Value;
            this.Property4 = item.Property4;
            this.Property4Value = item.Property4Value;
            this.Property5 = item.Property5;
            this.Property5Value = item.Property5Value;
            this.Property6 = item.Property6;
            this.Property6Value = item.Property6Value;
        }

        public void UpdateSearchInfoName()
        {
            string prefix = this.SearchInfoNamePrefix + char.ToString(_MARK);
            int index = this.SearchInfoName == null ? -1 : this.SearchInfoName.IndexOf(_MARK);
            if (index >= 0)
            {
                this.SearchInfoName = prefix + this.SearchInfoName.Substring(index + 1);
            }
            else
                this.SearchInfoName = prefix + this.SearchInfoName;
        }

        public string PropertyName
        {
            get
            {
                string b = Helper.GetName("裝備類型", this.EquipKind);
                if (this.ItemName == null || this.ItemName.Length == 0)
                {
                    string c = Helper.GetName(Helper.GetName("裝備類型", this.EquipKind) + "物品類型", this.ItemKind);
                    return b + c;
                }
                else
                {
                    return "傳奇物品" + b + char.ToString(_ITEM_NAME_LEFT) + this.ItemName + char.ToString(_ITEM_NAME_RIGHT);
                }
            }
        }

        public string SearchInfoNamePrefix
        {
            get
            {
                string a = Helper.GetName("職業", this.Occupation);
                string b = Helper.GetName("裝備類型", this.EquipKind);
                if (this.ItemName == null || this.ItemName.Length == 0)
                {
                    string c = Helper.GetName(Helper.GetName("裝備類型", this.EquipKind) + "物品類型", this.ItemKind);
                    return a + b + c;
                }
                else
                {
                    return "傳奇物品" + a + b + char.ToString(_ITEM_NAME_LEFT) + this.ItemName + char.ToString(_ITEM_NAME_RIGHT);
                }
            }
        }

        public static readonly DependencyProperty ItemNameProperty =
            DependencyProperty.Register("ItemName", typeof(string), typeof(SearchItem),
            new FrameworkPropertyMetadata("", new PropertyChangedCallback(_OnItemNameChanged)));
        static void _OnItemNameChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            if (Helper.GetMainWindow() != null && Helper.GetMainWindow().searchInformation != null)
                Helper.GetMainWindow().searchInformation.UpdateComboxAndPropertyItem();
        }
        [Editable]
        public string ItemName
        {
            get { return (string)GetValue(ItemNameProperty); }
            set { SetValue(ItemNameProperty, value); }
        }

        public static readonly DependencyProperty IsAvailableProperty =
        DependencyProperty.Register("IsAvailable", typeof(bool), typeof(SearchItem), null);
        [Editable]
        public bool IsAvailable
        {
            get { return (bool)GetValue(IsAvailableProperty); }
            set { SetValue(IsAvailableProperty, value); }
        }

        public static readonly DependencyProperty OccupationProperty =
                DependencyProperty.Register("Occupation", typeof(int), typeof(SearchItem),
                new FrameworkPropertyMetadata(0, new PropertyChangedCallback(_OnOccupationChanged)));
        static void _OnOccupationChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            SearchItem item = (SearchItem)sender;
            item.UpdateSearchInfoName();
        }
        [Editable]
        public int Occupation
        {
            get { return (int)GetValue(OccupationProperty); }
            set { SetValue(OccupationProperty, value); }
        }

        public static readonly DependencyProperty EquipKindProperty =
        DependencyProperty.Register("EquipKind", typeof(int), typeof(SearchItem),
        new FrameworkPropertyMetadata(0, new PropertyChangedCallback(_OnEquipChanged)));
        static void _OnEquipChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            if ((int)e.NewValue != -1 && Helper.GetMainWindow() != null && Helper.GetMainWindow().searchInformation != null)
                Helper.GetMainWindow().searchInformation.UpdateComboxAndPropertyItem();
        }
        [Editable]
        public int EquipKind
        {
            get { return (int)GetValue(EquipKindProperty); }
            set { SetValue(EquipKindProperty, value); }
        }

        public static readonly DependencyProperty ItemKindProperty =
            DependencyProperty.Register("ItemKind", typeof(int), typeof(SearchItem),
            new FrameworkPropertyMetadata(0, new PropertyChangedCallback(_OnItemChanged)));
        static void _OnItemChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            if ((int)e.NewValue != -1 && Helper.GetMainWindow() != null && Helper.GetMainWindow().searchInformation != null)
                Helper.GetMainWindow().searchInformation.UpdateComboxAndPropertyItem();
        }
        [Editable]
        public int ItemKind
        {
            get { return (int)GetValue(ItemKindProperty); }
            set { SetValue(ItemKindProperty, value); }
        }

        public static readonly DependencyProperty ItemRarityProperty =
            DependencyProperty.Register("ItemRarity", typeof(int), typeof(SearchItem), null);
        [Editable]
        public int ItemRarity
        {
            get { return (int)GetValue(ItemRarityProperty); }
            set { SetValue(ItemRarityProperty, value); }
        }

        public static readonly DependencyProperty SearchInfoNameProperty =
            DependencyProperty.Register("SearchInfoName", typeof(string), typeof(SearchItem),
                        new FrameworkPropertyMetadata(null, new PropertyChangedCallback(_OnSearchItemNameChanged)));
        static void _OnSearchItemNameChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            SearchItem item = (SearchItem)sender;
            item.UpdateSearchInfoName();
        }
        [Editable]
        public string SearchInfoName
        {
            get { return (string)GetValue(SearchInfoNameProperty); }
            set { SetValue(SearchInfoNameProperty, value); }
        }

        public static readonly DependencyProperty ItemBuyPriceProperty =
            DependencyProperty.Register("ItemBuyPrice", typeof(int), typeof(SearchItem), null);
        [Editable]
        public int ItemBuyPrice
        {
            get { return (int)GetValue(ItemBuyPriceProperty); }
            set { SetValue(ItemBuyPriceProperty, value); }
        }

        public static readonly DependencyProperty ItemAuctionPriceProperty =
            DependencyProperty.Register("ItemAuctionPrice", typeof(int), typeof(SearchItem), null);
        [Editable]
        public int ItemAuctionPrice
        {
            get { return (int)GetValue(ItemAuctionPriceProperty); }
            set { SetValue(ItemAuctionPriceProperty, value); }
        }

        public static readonly DependencyProperty Property1Property =
            DependencyProperty.Register("Property1", typeof(int), typeof(SearchItem), null);
        [Editable]
        public int Property1
        {
            get { return (int)GetValue(Property1Property); }
            set { SetValue(Property1Property, value); }
        }

        public static readonly DependencyProperty Property2Property =
            DependencyProperty.Register("Property2", typeof(int), typeof(SearchItem), null);
        [Editable]
        public int Property2
        {
            get { return (int)GetValue(Property2Property); }
            set { SetValue(Property2Property, value); }
        }

        public static readonly DependencyProperty Property3Property =
            DependencyProperty.Register("Property3", typeof(int), typeof(SearchItem), null);
        [Editable]
        public int Property3
        {
            get { return (int)GetValue(Property3Property); }
            set { SetValue(Property3Property, value); }
        }

        public static readonly DependencyProperty Property4Property =
            DependencyProperty.Register("Property4", typeof(int), typeof(SearchItem), null);
        [Editable]
        public int Property4
        {
            get { return (int)GetValue(Property4Property); }
            set { SetValue(Property4Property, value); }
        }

        public static readonly DependencyProperty Property5Property =
            DependencyProperty.Register("Property5", typeof(int), typeof(SearchItem), null);
        [Editable]
        public int Property5
        {
            get { return (int)GetValue(Property5Property); }
            set { SetValue(Property5Property, value); }
        }

        public static readonly DependencyProperty Property6Property =
            DependencyProperty.Register("Property6", typeof(int), typeof(SearchItem), null);
        [Editable]
        public int Property6
        {
            get { return (int)GetValue(Property6Property); }
            set { SetValue(Property6Property, value); }
        }

        public static readonly DependencyProperty Property1ValueProperty =
    DependencyProperty.Register("Property1Value", typeof(float), typeof(SearchItem), null);
        [Editable]
        public float Property1Value
        {
            get { return (float)GetValue(Property1ValueProperty); }
            set { SetValue(Property1ValueProperty, value); }
        }

        public static readonly DependencyProperty Property2ValueProperty =
            DependencyProperty.Register("Property2Value", typeof(float), typeof(SearchItem), null);
        [Editable]
        public float Property2Value
        {
            get { return (float)GetValue(Property2ValueProperty); }
            set { SetValue(Property2ValueProperty, value); }
        }

        public static readonly DependencyProperty Property3ValueProperty =
            DependencyProperty.Register("Property3Value", typeof(float), typeof(SearchItem), null);
        [Editable]
        public float Property3Value
        {
            get { return (float)GetValue(Property3ValueProperty); }
            set { SetValue(Property3ValueProperty, value); }
        }

        public static readonly DependencyProperty Property4ValueProperty =
            DependencyProperty.Register("Property4Value", typeof(float), typeof(SearchItem), null);
        [Editable]
        public float Property4Value
        {
            get { return (float)GetValue(Property4ValueProperty); }
            set { SetValue(Property4ValueProperty, value); }
        }

        public static readonly DependencyProperty Property5ValueProperty =
            DependencyProperty.Register("Property5Value", typeof(float), typeof(SearchItem), null);
        [Editable]
        public float Property5Value
        {
            get { return (float)GetValue(Property5ValueProperty); }
            set { SetValue(Property5ValueProperty, value); }
        }

        public static readonly DependencyProperty Property6ValueProperty =
            DependencyProperty.Register("Property6Value", typeof(float), typeof(SearchItem), null);
        [Editable]
        public float Property6Value
        {
            get { return (float)GetValue(Property6ValueProperty); }
            set { SetValue(Property6ValueProperty, value); }
        }
    }
}
